
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace Dragoon
{
    /// <summary>
    /// Measures and shows the frames per second
    /// </summary>
    public partial class FPSCounter : Microsoft.Xna.Framework.DrawableGameComponent
    {
        double _deltaFPSTime = 2.0f;
        double _lastFps = 0;
        float _fontHeight = 0;
        double _frameCount = 0;
        SpriteBatch _batch;
        SpriteFont _font;
        

        public FPSCounter(Game game)
            : base(game)
        {
            DrawOrder = int.MaxValue;
        }

        protected override void LoadContent()
        {
            ContentManager mgr = new ContentManager(Game.Services);
            {
                _font = mgr.Load<SpriteFont>("content/normal");
                _fontHeight = _font.MeasureString("X").Y;
                _batch = new SpriteBatch(GraphicsDevice);
            }
            base.LoadContent();
        }

        public override void Update(GameTime time)
        {
            if (_deltaFPSTime > 1.0f)
            {
                _lastFps = _frameCount / _deltaFPSTime;
                _deltaFPSTime = 0;
                _frameCount = 0;
            }
        }


        public override void Draw(GameTime time)
        {
            _frameCount++;
            _deltaFPSTime += time.ElapsedGameTime.TotalSeconds;
            _batch.Begin(
                SpriteBlendMode.AlphaBlend,
                SpriteSortMode.Deferred,
                SaveStateMode.SaveState);
            _batch.DrawString(_font,
                string.Format("{000:000.00} fps", _lastFps), 
                    new Vector2(10,_fontHeight), Color.Yellow);
            _batch.End();
        }
    }
}


